package com.zxkj.login.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.entity.ACache;
import com.czy.basecommon.utils.language.SPUtil;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.MobileTool;
import com.czy.basecommon.utils.tools.SPUtils;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.toast.XToast;
import com.zcw.togglebutton.ToggleButton;
import com.zxkj.login.BR;
import com.zxkj.login.R;
import com.zxkj.login.databinding.ActivityLoginBinding;
import com.zxkj.login.net.ResponseLogin;
import com.zxkj.login.viewmodel.activity.LoginViewModel;

import java.util.List;

@Route(path = "/login/LoginActivity")
public class LoginActivity extends BaseVmDataBindingActivity<ActivityLoginBinding,LoginViewModel> {


   private  boolean  isOffline;
    private MiniLoadingDialog dialog;

    @Override
    protected void initUI() {

        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                .constantRequest()
                // 支持请求6.0悬浮窗权限8.0请求安装权限
                //.permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES)
                // 不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {

                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll){


//                         //加载fragment
//                          initFragment();
//                          addFragment();


                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {

                    }
                });
        binding.btnStatus.setToggleOn();
        binding.tbOff.setVisibility(View.GONE);
        binding.tbOn.setVisibility(View.VISIBLE);
        binding.btnStatus.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    isOffline=false;

                    binding.tbOff.setVisibility(View.GONE);
                    binding.tbOn.setVisibility(View.VISIBLE);
                }else{

                    isOffline=true;
                    binding.tbOn.setVisibility(View.GONE);
                    binding.tbOff.setVisibility(View.VISIBLE);
                }
            }
        });




    }


    @Override
    protected void setMyStyle() {
        super.setMyStyle();



    }

    @Override
    protected void initData() {
        if (!TextUtils.isEmpty(SPUtils.get(ItrackConstant.TOKEN,""))){
            ARouter.getInstance().build("/Common/SelectProjectActivity").navigation();
            ActivityTool.getInstance().popOneActivity(this);
        }

        binding.edName.setTextEx(SPUtils.get(ItrackConstant.LOGINID,""));
    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.data.observe(this, new Observer<StateData<String>>() {
            @Override
            public void onChanged(StateData<String> stringStateData) {
               if ( stringStateData.getStatus()==StateData.DataStatus.SUCCESS){
                   Log.i("SUCCESS",stringStateData.getData());
               }else{
                   Log.e("Error",stringStateData.getMsg());
               }
            }
        });


    }

    @Override
    protected void setListener() {
       binding.mbGoLogin.setOnClickListener(v->{

//           if (isOffline){
//               SPUtils.put("onLine","2");
//               startActivity(SynActivity.class);
//               ActivityTool.getInstance().popOneActivity(this);
//           }else{
//               SPUtils.put("onLine","1");
////               startActivity(Employee_Information_Activity.class);
               if (TextUtils.isEmpty(binding.edName.getTextEx().toString())){
                   XToast.error(LoginActivity.this,"賬號不能為空").show();
                   return;
               }
           if (TextUtils.isEmpty(binding.edPwd.getTextEx().toString())){
               XToast.error(LoginActivity.this,"密碼不能為空").show();
               return;
           }
           dialog =new MiniLoadingDialog(this,"加載中...");
           dialog.show();
   viewModel.login(binding.edName.getTextEx(),binding.edPwd.getTextEx());


//           }

//           ActivityTool.getInstance().popOneActivity(this);
//           SPUtils.put("radom", 1);
//           ActivityTool.getInstance().popOneActivity(LoginActivity.this);
//           ARouter.getInstance().build("/Line/LinePatrolActivity").navigation();
//           viewModel.upData();
       });
        viewModel.login.observe(this, new Observer<StateData<ResponseLogin>>() {
            @Override
            public void onChanged(StateData<ResponseLogin> responseLoginStateData) {
                if (responseLoginStateData.getStatus()== StateData.DataStatus.SUCCESS){
                    dialog.dismiss();
                    SPUtils.put(ItrackConstant.LOGINID,binding.edName.getTextEx().toString());
                    if (responseLoginStateData.getData()!=null){
                        SPUtils.put(ItrackConstant.TOKEN,responseLoginStateData.getData().getToken());
                        XToast.success(LoginActivity.this,"登錄成功").show();
                        ARouter.getInstance().build("/Common/SelectProjectActivity").navigation();
                        ActivityTool.getInstance().popOneActivity(LoginActivity.this);
                    }else{
                        XToast.error(LoginActivity.this,"登錄失敗").show();
                    }


                }else{
                    dialog.dismiss();

                    switch (responseLoginStateData.getCode()){
                        case 403:
                            XToast.error(LoginActivity.this,"身法驗證信息出錯").show();
                            break;
                        case 406:
                            XToast.error(LoginActivity.this,"密碼錯誤").show();
                            break;
                        case 411 :
                            XToast.error(LoginActivity.this,"長號不存在").show();
                            break;
                        case 408 :
                            XToast.error(LoginActivity.this,"角色不對").show();
                            break;
                        default:
                            XToast.error(LoginActivity.this,"登錄失敗").show();
                            break;

                   }


                }
            }
        });

    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.mLoginViewModel;
    }


}
